<script>
import axios from 'axios';
import {defineComponent} from 'vue'
import store from "@/stores/store";

export default defineComponent({
    name: "nav-head",
  computed: {
    store() {
      return store
    }
  },
    data(){
      return{
        searchContent:'',
        products:[]
      }
    },
    methods:{
      searchProduct(content){
        if(content){
          const promise=this.searchProduct_axios(content);
        promise
        .then(
            response => {
                this.products=response.data.data
                console.log(this.products)
            }
        )
        .catch((error)=>{
            console.error('异常',error)
        })
        }
        else{
          this.products=''
        }
        
      },
      searchProduct_axios(content){
          return axios({
            url:'http://localhost:8080/searchProduct',
		        method:'GET',
		        //withCredentials: true,
		        params:{
		          searchContent:this.searchContent
		        }
          })
      },
      toProductInfo(productId){
        this.$router.push({name:'picinfo',query:{productId:productId}});
      }
    }
})
</script>

<template>
    <div class="nav-top">
        <div class="w">
            <div class="user-info">
              <span class="user not-login">
        <!-- 此处作为判断的第一个地方，判断是否已经登录 -->
                <span class="link" v-if="store.state.account === null">
                  <router-link to="/login">登录</router-link>
                </span>
<!--        判断登录-->
<!--        判断是否以及登录-->
                <span class="link" v-if="store.state.account !== null">
                  <a href="#">退出登录</a>
                </span>
<!--        判断登录-->
                <span class="link">
                  <router-link to="/register">注册</router-link>
                </span>
              </span>
            </div>
            <ul class="nav-top-list">
                <li class="nav-top-item">
                    <router-link to="/index/carts" class="link">
                        <i class="fa fa-shopping-cart"></i>
                        购物车
                    </router-link>
                </li>
<!--                登陆后显示-->
                    <li class="nav-top-item">
                        <router-link to="/index/account">个人中心</router-link>
                    </li>
<!--                登陆后显示-->
                <li class="nav-top-item">
                    <a href="#" class="link">关于myStore</a>
                </li>
            </ul>
        </div>
    </div>

    <div class="nav-search">
        <div class="w">
            <!-- 此处也可以考虑贴图在这里 -->
            <a href="#" class="link logo">MyStore</a>
            <div class="search-content">
                <label>
                    <input type="text" class="search-input" placeholder="请输入商品名称" id="keyword" v-model="searchContent" @keyup="searchProduct(this.searchContent)"/>
                </label>
                <button class="myBtn search-btn">搜索</button>
            </div>
        </div>
        <!--  搜索框内容   -->
        <div id="productAutoComplete" v-if="searchContent">
            <ul id="productAutoList" >
              <li class="productAutoItem" v-for="product in products" v-if="searchContent" @click="toProductInfo(product.productId)">{{ product.categoryName+':'+product.name }}</li>
            </ul>
        </div>
    </div>
</template>

<style scoped>
.w{
  width: 1080px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}
.link{
  color: #999;
  cursor: pointer;
  text-decoration: none;
}
a{
  font-size: 14px;
  text-decoration: none;
  color: #999;
}
a:hover{
  color: #215496;
}
.link:hover{
  color: #215496;
}

.myBtn{
  display: inline-block;
  padding: 0 20px;
  height: 40px;
  line-height: 40px;
  vertical-align: middle;
  border: none;
  background: #215496;
  color: #fff;
}

.nav-top{
  background: #eee;
  height: 50px;
  line-height: 50px;
}

.nav-top .user-info{
  float: left;
}

.nav-top .user-info .link{
  margin-right: 5px;
}

.nav-top .nav-top-list{
  float: right;
}

.nav-top .nav-top-list .nav-top-item{
  display: inline-block;
  margin-left: 5px;
}

.nav-search .logo{
  position: absolute;
  left: 50px;
  top: 32px;
  display: block;
  font-size: 36px;
  font-weight: bold;
  color: #215496;
}

.nav-search .search-content{
  padding: 40px 0 30px 250px;
}

.nav-search .search-content .search-input{
  width: 500px;
  height: 40px;
  line-height: 40px;
  font-size: 18px;
  border: 2px solid #215496;
  outline: none;
}

.nav-search .search-content .search-btn{
  position: absolute;
  width: 120px;
  height: 40px;
  font-size: 20px;
  cursor: pointer;
}

#productAutoComplete{
    background: #fff;
    position: absolute;
    top: 129px;
    left: 470px;
    width: 499px;
    z-index: 999;
    border: 2px solid #215496;
    border-top: none;
}

#productAutoList{
    margin: 0;
    padding: 0;
    border: 1px solid #aaa;
}

.productAutoItem{
    list-style: none;
    height: 30px;
    line-height: 28px;
    font-size: 25px;
    text-align: left;
    padding-left: 3px;
    margin-top: 3px;
    border-bottom: 1px solid #666;
    cursor: pointer;
}

</style>